package com.xuecheng.api.content.controller;

import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.common.domain.page.PageRequestParams;
import com.xuecheng.api.common.domain.page.PageVO;
import com.xuecheng.api.common.util.SecurityUtil;
import com.xuecheng.api.content.CourseBaseApi;

import com.xuecheng.api.content.convert.CourseBaseConvert;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBseVo;
import com.xuecheng.api.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {
    @Autowired
    private CourseBaseService courseBaseService;

    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,
                                  @RequestBody QueryCourseModel model) {

        // 解析请求头并获得公司id
        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.queryCourseList(params, model, companyId);
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBseVo courseBseVo){
        CourseBaseDTO courseBaseDTO=CourseBaseConvert.INSTANCE.vo2dto(courseBseVo);
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.createCourseBase(courseBaseDTO);
    }


    @GetMapping(value="course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO=courseBaseService.getCourseBaseById(courseBaseId,companyId);
        return  courseBaseDTO;

    }

    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBseVo courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.modifyCourseBaseById(courseBaseDTO);

    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBaseById(@PathVariable Long courseBaseId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }
}